﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WebcrawlerController;
using Graph;
using System.Threading.Tasks;


using Webcrawler;

namespace GUI
{
    public partial class Main : Form
    {
        private WebCrawlerCTR _webCrawlerCTR = new WebCrawlerCTR();
        
        public Main()
        {
            InitializeComponent(); 
        }

        //public void PrintTree(Vertex v)
        //{
        //    IBasicGraph _graph = new BasicGraph();
        //    Queue<Vertex> q = new Queue<Vertex>();
        //    v.Mark = true;
        //    q.Enqueue(v);
        //    while (q.Count > 0)
        //    {
        //        Vertex w = q.Dequeue();
        //        foreach (Vertex u in _graph.GetAdjacencies(w))
        //        {
        //            if (!u.Mark)
        //            {

        //                u.Mark = true;
        //                q.Enqueue(u);
        //            }
        //        }
        //    }
        //}

        public TreeNode reku(Link link, TreeNode treeNode)
        {
            List<Link> links = link.LinkList;
            string name = link.Name + " <" + link.Url + ">";
            if(links.Count > 0) {
                name += ", " + links.Count + " link" + (links.Count != 1 ? "s" : "");
            }

            TreeNode treeNode2 = treeNode.Nodes.Add(name);
            
            foreach (Link link2 in link.LinkList)
            {
                treeNode2.Nodes.Add(reku(link2, treeNode));
            }

            return treeNode2;
        }


        //public void structureload()
        //{
        //    treeGraph.BeginUpdate();
        //    treeGraph.Nodes.Clear();
        //    Link link = new Link();
            //reku(link);
            //treeGraph.Nodes.Add("Main");


            //foreach (Link link in _webCrawlerCTR.ListLink)
            //{
            //    treeGraph.Nodes.Add(link.Name + " (" + link.Url + ")");

            //    foreach (Link link2 in link.LinkList)
            //    {
            //        treeGraph.Nodes[_webCrawlerCTR.ListLink.IndexOf(link)].Nodes.Add(new TreeNode(link2.Name + " (" + link2.Url + ")"));
            //    }
                
            //}
            //Cursor.Current = Cursors.Default;
            //treeGraph.EndUpdate();
            
            
            //TreeNode treeNode = new TreeNode("main");
            //treeGraph.Nodes.Add(treeNode);
            //TreeNode Node1 = new TreeNode("test1");
            //TreeNode Node2 = new TreeNode("test2");

            //TreeNode[] Array = new TreeNode[] { Node1, Node2 };

            //treeNode = new TreeNode("parent", Array);
            //treeGraph.Nodes.Add(treeNode);
            //foreach (var item in collection)
            //{
         
            //}
            
            //foreach (SortedList<int, string> item in _webCrawlerCTR.GetLevelList())
            //{
            //    List<Vertex> nV = new List<Vertex>();
            //    for (int i = 0; i < item.Count; i++)
            //    {
            //        nV.Add(item[i]);
            //        item[i].add()
            //    }
            //    TreeNode treeNode = new TreeNode("");
            //    treeGraph.Nodes.Add(treeNode);

            //    treeNode = new TreeNode("parent", item);
            //}

        //}


        private void goBtn_Click(object sender, EventArgs e)
        {

               this.CrawlSite();

        }

        private void urlField_KeyUp(object sender, KeyEventArgs e)
        {
            // On enter
            if (e.KeyValue == 13)
            {
                this.CrawlSite();
            }
        }

        private void CrawlSite() {
            string url = this.urlField.Text;
            int levels = (int)this.levelsNum.Value;
            bool thisDomainOnly = this.thisDomainOnly.Checked ? true : false ;

            if (url.Length > 0)
            {
                Link l = _webCrawlerCTR.Start(url, levels, thisDomainOnly);
                this.headerText.Text = _webCrawlerCTR.Headers;
                this.htmlText.Text = _webCrawlerCTR.Html;
                this.webBrowser.Url = new Uri(l.Url);

                //TreeNodeCollection nodes = treeGraph.Nodes;
                TreeNode node = new TreeNode();
                treeGraph.Nodes.Clear();
                treeGraph.Nodes.Add(reku(l, node));
                
                
                    //structureload();

                //Crawler crawler = new Crawler();
                //if (crawler.CrawlPage(url, levels, thisDomainOnly) >= 0)
                //{
                //    tabControl.Enabled = true;

                //    htmlText.Text = crawler.Content;
                //    headerText.Text = crawler.Headers;

                //string links = "";
                //int i = 1;
                ////foreach (Vertex l in _webCrawlerCTR.GetGraph())
                ////{
                //    links += "#" + i + ": " + l.Link.Url + " => \"" + l.Link.Name + "\"\n";
                //    i++;
                //}

                //linksText.Text = links;

                //    webBrowser.Url = new Uri(crawler.Url);
                //}
            }
        }

        private void progressBar1_Click(object sender, EventArgs e)
        {

        }



    }
}
